package com.jiangcheng.demo.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangcheng.demo.entity.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiangcheng.demo.utils.FindArticleVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2021-07-18
 */
public interface ArticleMapper extends BaseMapper<Article> {
    // 对文章进行分页查询 并且查出用户头像和名词 并且进行用户过滤
    @Select("SELECT is_public,art_id,title,content,create_time,comment_count,username,u_pic,article_pic " +
            "FROM article,user WHERE user.id != #{filterUId} AND user.id = article.author_id AND article_state = 0 limit #{start},#{pagesize}")
    List<FindArticleVo> findArticleByPage(@Param("start") int start,@Param("pagesize") int pagesize,@Param("filterUId") int filterUId);
    // 根据用户ID（author_id）查询文章
    @Select("SELECT * FROM article where author_id=#{authorId} AND article_state = 0")
    List<Article> findArticleByAuthorId(@Param("authorId") int authorId);
    // 查询用户的文章条数
    @Select("SELECT count(*) FROM article where author_id=#{authorId} AND article_state = 0")
    Integer findCountByAuthorId(@Param("authorId") int authorId);

    // 根据文章ID查询文章信息以及作者头像和作者名字
    @Select("SELECT user.id as uId,is_public,art_id,title,content,create_time,comment_count,username,u_pic,article_pic " +
            "FROM user inner join article ON user.id = article.author_id where article.art_id = #{artId}")
    FindArticleVo findArticleInfoById(@Param("artId") int artId);

    @Update("UPDATE article SET article_state=1 WHERE art_id=#{artId}")
    // 进行文章软删除 把状态改变1
    Integer deleteArticleById(int artId);
}
